﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public static class UnityListPools
{
    static Dictionary<System.Type, Queue<IList>> queueMap = new Dictionary<System.Type, Queue<IList>>();
    public static Queue<IList> GetPool(System.Type t)
    {
        Queue<IList> queue = null;
        if (!queueMap.TryGetValue(t, out queue))
        {
            queue = new Queue<IList>();
            queueMap.Add(t, queue);
        }

        return queue;
    }

    static public void Recycle(IList l)
    {
        // l.Clear();
        // Queue<IList> q = UnityListPools.GetPool(l.GetType());
        // q.Enqueue(l);
    }
}

public static class UnityListPool<T> where T : class
{
    static Queue<IList> listQueue;

    static UnityListPool()
    {
        listQueue = UnityListPools.GetPool(typeof(List<T>));
    }

    static public List<T> Create()
    {
        List<T> l = null;
        if (listQueue.Count > 0)
        {
            l = listQueue.Dequeue() as List<T>;
        }
        else
        {
            l = new List<T>();
        }

        return l;
    }

    static public void Recycle(List<T> l)
    {
        // l.Clear();
        // listQueue.Enqueue(l);
        // l = null;
    }
}

public static class ListExtensionsForListPool
{
    static public void RecycleToListPool<T>(this List<T> l) where T : class
    {
        UnityListPool<T>.Recycle(l);
    }
}

//public static class GetComponentsExHelper
//{
//    static public List<Transform> GetTransfromsInChildren(bool includeInactive)
//    {
//        
//    }
////    static public List<Component> Create()
////    {
////        return new List<Component>();
////    }
//}